home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / samples / line.c.z / line.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  3.2 KB  |  194 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "tk.h"
  5.  
  6.  
  7. #define CI_OFFSET 16
  8.  
  9.  
  10. GLenum rgb, doubleBuffer, directRender, windType;
  11.  
  12. GLenum mode1, mode2;
  13. GLint size;
  14. float pntA[3] = {
  15.     -160.0, 0.0, 0.0
  16. };
  17. float pntB[3] = {
  18.     -130.0, 0.0, 0.0
  19. };
  20. float pntC[3] = {
  21.     -40.0, -50.0, 0.0
  22. };
  23. float pntD[3] = {
  24.     30.0, 60.0, 0.0
  25. };
  26.  
  27.  
  28. static void Init(void)
  29. {
  30.     GLint i;
  31.  
  32.     glClearColor(0.0, 0.0, 0.0, 0.0);
  33.  
  34.     glLineStipple(1, 0xF0E0);
  35.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  36.  
  37.     if (!rgb) {
  38.     for (i = 0; i < 16; i++) {
  39.         tkSetOneColor(i+CI_OFFSET, i/15.0, i/15.0, 0.0);
  40.     }
  41.     }
  42.  
  43.     mode1 = GL_FALSE;
  44.     mode2 = GL_FALSE;
  45.     size = 1;
  46. }
  47.  
  48. static void Reshape(int width, int height)
  49. {
  50.  
  51.     glViewport(0, 0, (GLint)width, (GLint)height);
  52.  
  53.     glMatrixMode(GL_PROJECTION);
  54.     glLoadIdentity();
  55.     gluOrtho2D(-175, 175, -175, 175);
  56.     glMatrixMode(GL_MODELVIEW);
  57. }
  58.  
  59. static GLenum Key(int key, GLenum mask)
  60. {
  61.  
  62.     switch (key) {
  63.       case TK_ESCAPE:
  64.     tkQuit();
  65.       case TK_1:
  66.     mode1 = !mode1;
  67.     break;
  68.       case TK_2:
  69.     mode2 = !mode2;
  70.     break;
  71.       case TK_W:
  72.     size++;
  73.     break;
  74.       case TK_w:
  75.     size--;
  76.     if (size < 1) {
  77.         size = 1;
  78.     }
  79.     break;
  80.       default:
  81.     return GL_FALSE;
  82.     }
  83.     return GL_TRUE;
  84. }
  85.  
  86. static void Draw(void)
  87. {
  88.     GLint ci, i;
  89.  
  90.     glClear(GL_COLOR_BUFFER_BIT);
  91.  
  92.     glLineWidth(size);
  93.  
  94.     if (mode1) {
  95.     glEnable(GL_LINE_STIPPLE);
  96.     } else {
  97.     glDisable(GL_LINE_STIPPLE);
  98.     }
  99.     
  100.     if (mode2) {
  101.     ci = CI_OFFSET;
  102.     glEnable(GL_LINE_SMOOTH);
  103.     glEnable(GL_BLEND);
  104.     } else {
  105.     ci = TK_YELLOW;
  106.     glDisable(GL_LINE_SMOOTH);
  107.     glDisable(GL_BLEND);
  108.     }
  109.  
  110.     glPushMatrix();
  111.  
  112.     for (i = 0; i < 360; i += 5) {
  113.     glRotatef(5.0, 0,0,1);
  114.  
  115.     (rgb) ? glColor3f(1.0, 1.0, 0.0) : glIndexi(ci);
  116.     glBegin(GL_LINE_STRIP);
  117.         glVertex3fv(pntA);
  118.         glVertex3fv(pntB);
  119.     glEnd();
  120.  
  121.     glPointSize(1);
  122.  
  123.     TK_SETCOLOR(windType, TK_GREEN);
  124.     glBegin(GL_POINTS);
  125.         glVertex3fv(pntA);
  126.         glVertex3fv(pntB);
  127.     glEnd();
  128.     }
  129.  
  130.     glPopMatrix();
  131.  
  132.     glFlush();
  133.  
  134.     if (doubleBuffer) {
  135.     tkSwapBuffers();
  136.     }
  137. }
  138.  
  139. static GLenum Args(int argc, char **argv)
  140. {
  141.     GLint i;
  142.  
  143.     rgb = GL_TRUE;
  144.     doubleBuffer = GL_FALSE;
  145.     directRender = GL_TRUE;
  146.  
  147.     for (i = 1; i < argc; i++) {
  148.     if (strcmp(argv[i], "-ci") == 0) {
  149.         rgb = GL_FALSE;
  150.     } else if (strcmp(argv[i], "-rgb") == 0) {
  151.         rgb = GL_TRUE;
  152.     } else if (strcmp(argv[i], "-sb") == 0) {
  153.         doubleBuffer = GL_FALSE;
  154.     } else if (strcmp(argv[i], "-db") == 0) {
  155.         doubleBuffer = GL_TRUE;
  156.     } else if (strcmp(argv[i], "-dr") == 0) {
  157.         directRender = GL_TRUE;
  158.     } else if (strcmp(argv[i], "-ir") == 0) {
  159.         directRender = GL_FALSE;
  160.     } else {
  161.         printf("%s (Bad option).\n", argv[i]);
  162.         return GL_FALSE;
  163.     }
  164.     }
  165.     return GL_TRUE;
  166. }
  167.  
  168. void main(int argc, char **argv)
  169. {
  170.  
  171.     if (Args(argc, argv) == GL_FALSE) {
  172.     tkQuit();
  173.     }
  174.  
  175.     tkInitPosition(0, 0, 300, 300);
  176.  
  177.     windType = (rgb) ? TK_RGB : TK_INDEX;
  178.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  179.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  180.     tkInitDisplayMode(windType);
  181.  
  182.     if (tkInitWindow("Line Test") == GL_FALSE) {
  183.     tkQuit();
  184.     }
  185.  
  186.     Init();
  187.  
  188.     tkExposeFunc(Reshape);
  189.     tkReshapeFunc(Reshape);
  190.     tkKeyDownFunc(Key);
  191.     tkDisplayFunc(Draw);
  192.     tkExec();
  193. }
  194.